<?php
// +-----------------------+
// | 注释不留名，代码随便用 |
// +-----------------------+


namespace addons;


use library\tools\Http;
use think\Db;

abstract class Addons
{
    protected $menus = ['菜单名' => 'url'];

    abstract public function install();


    abstract public function uninstall();

    /**
     * 安装菜单
     * @param $menus
     * @param int $pid
     */
    protected function installMenu($menus, $pid = 0)
    {
        foreach ($menus as $title => $menu) {

            if (is_array($menu)) {
                $id = Db::name('system_menu')->insertGetId([
                    'title' => $title,
                    'pid' => $pid,
                    'url' => '',
                ]);
                $this->installMenu($menu, $id);
            } else {
                Db::name('system_menu')->insert([
                    'title' => $title,
                    'pid' => $pid,
                    'url' => $menu,
                    'icon' => $this->getMenuIcon()
                ]);
            }
        }
    }

    /**
     * 卸载菜单
     * @param $menus
     * @throws Exception
     * @throws exception\PDOException
     */
    protected function uninstallMenu($menus)
    {
        foreach ($menus as $title => $menu) {
            Db::name('system_menu')->where([
                'title' => $title,
                'url' => is_array($menu) ? '' : $menu,
            ])->delete();
            if (is_array($menu)) {
                $this->uninstallMenu($menu);
            }
        }
    }

    /**
     * 安装sql
     * @param $sql_file
     * @throws Exception
     */
    protected function installSql($sql_file)
    {
        if (!is_file($sql_file)) {
            return;
        }
        $sql = file_get_contents($sql_file);
        /*这里有个tp的巨坑，Db::execute();方法执行多个sql会报错
         *Db::execute最后是由PDOStatement对象execute()方法执行，执行中报错
         * 所以这里要拿到pdo对象，用pdo的exec()去执行。原理跟安装文件install.php不用tp内置db一样
         */
        $pdo = Db::connect()->getConnection()->getPdo();
        $pdo->exec($sql);
    }

    /**
     * 卸载sql
     * @param $sql_file
     */
    protected function uninstallSql($sql_file)
    {
        if (!is_file($sql_file)) {
            return;
        }
        $sql = file_get_contents($sql_file);
        preg_match_all('/DROP TABLE IF EXISTS `.*`/', $sql, $matches);
        if (isset($matches[0]) && !empty($matches[0])) {
            foreach ($matches[0] as $sql) {
                Db::execute($sql);
            }
        }
    }

    /**
     * 获取icon
     * @return string
     */
    public function getMenuIcon()
    {
        $url = url('admin/api.plugs/icon', '', '', true) . '?field=icon';
        $content = Http::get($url);
        preg_match_all('#<div class="icon-title">(.*)</div>#U', $content, $matches);
        if (isset($matches[1]) && !empty($matches[1])) {
            $icon = $matches[1][array_rand($matches[1])];
            return (strpos($icon, 'layui-icon') > -1 ? 'layui-icon' : 'fa') . ' ' . $icon;
        }
        return '';
    }


}